package com.es.test;


import com.es.bean.Employee;
import com.es.bean.NumberGuess;
import com.es.bean.Person;
import com.es.bean.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.Properties;

public class SpringEl {

    ClassPathXmlApplicationContext ctx = null;

    @Before
    public void before(){
        ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }

    @Test
    public void getBean(){

       /* for (int i = 0; i < 10; i++) {
            NumberGuess numberGuess = ctx.getBean(NumberGuess.class);
            System.out.println(numberGuess.getNumber());
        }*/

        NumberGuess numberGuess = ctx.getBean(NumberGuess.class);
        numberGuess.getSystemProperties().forEach((k,v)->{
            System.out.println(k+":"+v);
        });
        System.out.println("--------------------------------");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.stream(beanNames).forEach(System.out::println);

        Properties ps = System.getProperties();
        System.out.println("--------------------------------");
        ps.forEach((p,v)->{
            System.out.println(p+":"+v);
        });

    }

    @Test
    public void getPerson(){
        Person person = ctx.getBean(Person.class);
      //  System.out.println(person.getAddress());
        System.out.println(person.getDetailAddress());
    }

    @Test
    public void getEmployee(){
        Employee employee = ctx.getBean(Employee.class);
        System.out.println(employee.getAddress());
        if(employee.isCapital()){
            System.out.println("来自省会");
        }else{
            System.out.println("不是来自省会城市");
        }
    }

    @Test
    public void  getStudent(){
        Student student = ctx.getBean(Student.class);
        System.out.println(student.getName());
        System.out.println(student.getSubject());
    }

    @After
    public void destroy(){
        ctx.close();
    }

}
